package Dao.impl;

import Dao.AdminDao;
import Wrapper.OrderWrapper;
import entity.Orders;
import entity.Seat;
import entity.Train;
import entity.User;
import untils.Msg;
import untils.SqlCollection;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

public class AdminDaoImpl extends BaseDaoImpl implements AdminDao {
    private SqlCollection sqlCollection = new SqlCollection();
    private Msg<User> userMsg = new Msg<>();
    @Override
    public Msg<ArrayList<User>> findAllUserData() {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection conn = null;
        Msg<ArrayList<User>> users = new Msg<>();
        try{
            conn = sqlCollection.openConnection();

            String sql = "select * from user";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();

            ArrayList<User> addusers = new ArrayList<>();
            User user = null;
            while (rs.next()){
                user = new User();
                user.setUser_id(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setReal_name(rs.getString(4));
                user.setPhone(rs.getString(5));
                user.setEmail(rs.getString(6));
                user.setRole(rs.getString(7));
                addusers.add(user);
            }

            users.setData(addusers);
            if(addusers!=null){
                users.setMsg("SUCCEED");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst,rs);
        }

        return users;
    }

    @Override
    public Msg deleteUserData(int id) {

        PreparedStatement pst = null;
        Connection conn = null;
        Msg msg = new Msg();

        try{
            conn = sqlCollection.openConnection();
            String sql  = "delete from user where user_id = ?";
            pst = conn.prepareStatement(sql);
            pst.setInt(1,id);
            int res = pst.executeUpdate();
            if(res>0){
                msg.setMsg("SUCCEED");
            }else {
                msg.setMsg("ERROR");
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }
        return msg;
    }

    @Override
    public Msg<User> addUserData(User user1) {
        PreparedStatement pst = null;
        Connection conn = null;

        try{
            //1.连接数据库
            conn = sqlCollection.openConnection();
            String sql = "insert into user(username, password, real_name, phone, email,role) values(?,?,?,?,?,?)";
            pst = conn.prepareStatement(sql);
            pst.setString(1,user1.getUserName());
            pst.setString(2,user1.getPassword());
            pst.setString(3,user1.getReal_name());
            pst.setString(4,user1.getPhone());
            pst.setString(5,user1.getEmail());
            pst.setString(6,user1.getRole());
            int res =  pst.executeUpdate();

            if (res > 0) {
                userMsg.setMsg("SUCCEED");
            } else {
                userMsg.setMsg("ERROR");
            }

        }catch (Exception e ){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }
        return userMsg;
    }

    @Override
    public Msg updateTrainsDao(Train train) {
        PreparedStatement pst = null;
//        ResultSet rs = null;
        Connection conn = null;

        try{
            conn = sqlCollection.openConnection();
            String sql = "update trains set departure_time = ?,arrival_time = ?,start_station = ?,end_station = ? where train_id = ?";
            pst = conn.prepareStatement(sql);
            pst.setDate(1,train.getDepartureTime());
            pst.setDate(2,train.getArrivalTime());
            pst.setString(3,train.getStartStation());
            pst.setString(4,train.getEndStation());
            pst.setInt(5,train.getTrainId());
            pst.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }
        return null;
    }

    @Override
    public Msg<Seat> updateSeatData(String type,Seat seat) {
        PreparedStatement pst = null;
        Connection conn = null;
        Msg<Seat> msg = new Msg<>();
        try {
            conn = sqlCollection.openConnection();
            String sql = "update seat set price = ?,number = ? where train_id = ? and type = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,seat.getPrice());
            pst.setInt(2,seat.getNumber());
            pst.setInt(3,seat.getTrainId());
            pst.setString(4,type);
            int res = pst.executeUpdate();
            if(res>0){
                msg.setData(seat);
                msg.setMsg("SUCCEED");
            }else {
                msg.setMsg("ERROR");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return msg;
    }

    @Override
    public Msg<ArrayList<User>> findAllAdminData() {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection conn = null;
        Msg<ArrayList<User>> users = new Msg<>();
        try {
            conn = sqlCollection.openConnection();
            String sql = "select * from user where role = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,"管理员");
            rs = pst.executeQuery();
            ArrayList<User> addusers = new ArrayList<>();
            User user = null;
            while (rs.next()) {
                user = new User();
                user.setUser_id(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setReal_name(rs.getString(4));
                user.setPhone(rs.getString(5));
                user.setEmail(rs.getString(6));
                user.setRole(rs.getString(7));
                addusers.add(user);
            }
            users.setData(addusers);
            if (addusers != null) {
                users.setMsg("SUCCEED");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlCollection.closeConnection(conn, pst, rs);
        }

        return users;
    }

    @Override
    public int findUserByPhone(String phone){
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection conn = null;
        int userId = 0;
        try{
            conn = sqlCollection.openConnection();
            String sql = "select * from user where phone = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,phone);
            rs = pst.executeQuery();
            if(rs.next()){
                userId = rs.getInt("user_id");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst,rs);
        }
        return userId;
    }

    @Override
    public void addAdminData(int userId) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection conn = null;
        try {
            conn = sqlCollection.openConnection();
            String sql = "update user set role = ? where user_id = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,"管理员");
            pst.setInt(2,userId);
            int res = pst.executeUpdate();
            if (res>0){
                Msg.msg = "SUCCEED";
            }else {
                Msg.msg = "ERROR";
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }
    }

    @Override
    public void deleteAdminData(int userId) {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection conn = null;
        try {
            conn = sqlCollection.openConnection();
            String sql = "update user set role = ? where user_id = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,"普通用户");
            pst.setInt(2,userId);
            int res = pst.executeUpdate();
            if (res>0){
                Msg.msg = "SUCCEED";
            }else {
                Msg.msg = "ERROR";
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }
    }

    @Override
    public ArrayList<Orders> findAllOrderData() {
        PreparedStatement pst = null;
        ResultSet rs = null;
        Connection conn = null;
        Msg<ArrayList<Orders>> order = new Msg<>();
        ArrayList<Orders> ordersList = new ArrayList<>();
        try{
            conn = sqlCollection.openConnection();
            String sql = "select * from orders inner join tickets on orders.ticket_id = tickets.ticket_id inner join trains on orders.train_id = trains.train_id ";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            Orders orders = null;
            while (rs.next()){
                OrderWrapper.OrderWrapper(rs,ordersList);
            }
            order.setData(ordersList);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst,rs);
        }
        return ordersList;
    }

    @Override
    public void addTrain(Train train) {
        PreparedStatement pst = null;
//        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = sqlCollection.openConnection();
            String sql = "insert into trains(train_id,train_name,departure_time,arrival_time,start_station,end_station) values (?,?,?,?,?,?)";
            pst = conn.prepareStatement(sql);
            pst.setInt(1,train.getTrainId());
            pst.setString(2,train.getTrainName());
            pst.setDate(3,train.getDepartureTime());
            pst.setDate(4,train.getArrivalTime());
            pst.setString(5,train.getStartStation());
            pst.setString(6,train.getEndStation());
            int res = pst.executeUpdate();
            if(res>0){
                Msg.msg = "导入成功";
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }
    }

    @Override
    public void addSeat(Seat seat) {
        PreparedStatement pst = null;
//        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = sqlCollection.openConnection();
            String sql = "insert into seat(train_id,type,price,number,ticket_number) values (?,?,?,?,?)";
            pst = conn.prepareStatement(sql);
            pst.setInt(1,seat.getTrainId());
            pst.setString(2,seat.getType());
            pst.setString(3,seat.getPrice());
            pst.setInt(4,seat.getNumber());
            pst.setInt(5,seat.getTicketNumber());
            int res = pst.executeUpdate();
            if(res>0){
                Msg.msg = "导入成功";
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection(conn,pst);
        }
    }

    //导入车辆名字在到车辆管理中
    @Override
    public void addTrainName(String trainName) {
        PreparedStatement pst = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            conn = sqlCollection.openConnection();
            String sql = "insert into trainvehicleinfo(carname) values (?)";
            pst = conn.prepareStatement(sql);
            pst.setString(1,trainName);
            pst.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlCollection.closeConnection();
        }
    }


}
